package com.ctl.szmd.szmd.feature_wm.ui.adapter.wm

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import com.ctl.lib_common.base.mvvm.DataBindingViewHolder
import com.ctl.szmd.szmd.feature_wm.R
import com.ctl.szmd.szmd.feature_wm.data.model.WmDataListModel
import com.ctl.szmd.szmd.feature_wm.databinding.WmAdapterSyNewsBinding
import com.ctl.szmd.szmd.utils.ext.doWithTry

/**
 * created by : chentl
 * Date: 2020/07/28
 */
class WmHomeNewsAdapter :
    PagingDataAdapter<WmDataListModel, WmHomeNewsAdapter.MyViewHolder>(WmDataListModel.diffCallback) {
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        doWithTry {
            val data = getItem(position)
            data?.let {
                holder.bindData(data, position)
            }
        }
    }

    var listener: View.OnClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.wm_adapter_sy_news, parent, false)
        val holder = MyViewHolder(view)
        holder.itemView.setOnClickListener {
            listener?.onClick(it)
        }
        return holder
    }

    class MyViewHolder(view: View) :
        DataBindingViewHolder<WmDataListModel>(view) {
        val mBinding: WmAdapterSyNewsBinding by viewHolderBinding(view)
        override fun bindData(data: WmDataListModel, position: Int) {
            mBinding.apply {
                model = data
                executePendingBindings()
            }
            itemView.tag = data
        }
    }
}

